package com.alasbusy.alasbusyapp.activity;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.alasbusy.alasbusyapp.R;
import com.alasbusy.alasbusyapp.base.BaseActivity;
import com.alasbusy.alasbusyapp.utils.RegexUtils;
import com.zhy.android.percent.support.PercentLinearLayout;

public class UpdatePwdActivity extends BaseActivity implements View.OnClickListener {

    private EditText update_name;
    private Button update_send_btn;
    private EditText update_yzm;
    private EditText update_password;
    private TextView update_tishi;
    private PercentLinearLayout update_sure_btn;

    private int  time= 60;

    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            return false;
        }
    });

    @Override
    protected void initView() {
        PercentLinearLayout login_title = findViewById(R.id.update_title);
        setTop(login_title);
        getVisibility(true, this);
        getTitle("修改密码");

        update_name = findViewById(R.id.update_name);
        update_yzm = findViewById(R.id.update_yzm);
        update_send_btn = findViewById(R.id.update_send_btn);
        update_password = findViewById(R.id.update_password);

        update_tishi = findViewById(R.id.update_tishi);
        update_sure_btn = findViewById(R.id.update_sure_btn);

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        update_send_btn.setOnClickListener(this);
        update_sure_btn.setOnClickListener(this);
    }

    @Override
    public int getLayout() {
        return R.layout.activity_update_pwd;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.update_sure_btn:
                String update_password = this.update_password.getText().toString();
                if (!TextUtils.isEmpty(update_password)) {
                    if (RegexUtils.isPwdLegal(update_password)) {
                        update_tishi.setVisibility(View.INVISIBLE);

                    } else {
                        update_tishi.setVisibility(View.VISIBLE);
                        update_tishi.setText("由6-14位数字和字母组成");
                    }
                } else {
                    update_tishi.setVisibility(View.VISIBLE);
                    update_tishi.setText("密码不能为空.");
                }
                break;
            case R.id.update_send_btn:
                String update_name = this.update_name.getText().toString();
                if (RegexUtils.isChinaPhoneLegal(update_name)) {
                    update_tishi.setVisibility(View.INVISIBLE);
                    //通过后台发送手机验证码

                    //发送成功后
                    update_send_btn.setText("60s重发");
                    update_send_btn.setEnabled(false);
                    mHandler.postDelayed(yzmRunable,1000);
                } else {
                    update_tishi.setVisibility(View.VISIBLE);
                    update_tishi.setText("手机号码不正确");
                }
                break;
        }
    }

    Runnable yzmRunable = new Runnable() {
        @Override
        public void run() {
            if(time == 1) {
                update_send_btn.setText("发送验证码");
                update_send_btn.setEnabled(true);
                mHandler.removeCallbacks(this);
            }else {
                time = time - 1;
                update_send_btn.setText(time+"s重发");
                mHandler.postDelayed(yzmRunable,1000);
            }
        }
    };

}
